﻿using System;
using System.Linq;

namespace NC.Data
{
    public class AddressRepository
    {
        private readonly NEPALICORNEREntities _ncEntities;
        public AddressRepository()
        {
            _ncEntities = new NEPALICORNEREntities();
        }

        public int SaveAddress(Address address)
        {
            try
            {
                _ncEntities.Addresses.Add(address);
                _ncEntities.SaveChanges();
                return address.Id;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public System.Collections.Generic.IList<Country> GetAllCountries()
        {
            return _ncEntities.Countries.ToList();
        }

        public System.Collections.Generic.IList<State> GetStateByCountryId(int countryId)
        {
            return _ncEntities.States.Where(x => x.CountryId == countryId).ToList();
        }

        public System.Collections.Generic.IList<City> GetCityByStateId(int stateId)
        {
            var cities =  _ncEntities.Cities.Where(x => x.StateId == stateId);
            var uniqueCities = cities.GroupBy(x => x.CityName).Select(x => x.FirstOrDefault());
            return uniqueCities.OrderBy(x=> x.CityName).ToList();
        }
    }
}